package com.ys.otms.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ys.otms.dao.IFindShipFeedbackDao;
import com.ys.otms.dto.ParamDto;
import com.ys.otms.services.IDeductionsServices;
import com.ys.otms.services.IFindShipFeedbackServices;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.PageUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/19.
 */
@Controller
@RequestMapping("/feedback")
public class FindShipFeedbackController {
    private static Logger logger = LoggerFactory.getLogger(FindShipFeedbackController.class);
    @Autowired
    private IFindShipFeedbackServices iFindShipFeedbackServices;
    @Autowired
    private IDeductionsServices iDeductionsServices;

    /**
     * 立即联系(查询电话号码)
     * @param request
     * @return
     */
    @RequestMapping("/selectShipFeedbackByPhone")
    @ResponseBody
    public JSONObject selectShipFeedbackByPhone(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("FindShipFeedbackController selectShipFeedbackByPhone start");
        JSONObject jsonFeedback = new JSONObject();
        try {
            JSONObject jsonObject = iDeductionsServices.deductionsMethod(request,obj);
            if ("true".equals(jsonObject.getString("SUCCESS"))){
                jsonFeedback = iFindShipFeedbackServices.selectShipFeedbackByPhone(request,obj);
            }
        } catch (Exception e) {
            logger.error("FindShipFeedbackController selectShipFeedbackByPhone error", e);
            jsonFeedback.put("SUCCESS",false);
            jsonFeedback.put("MSG","查询数据失败！");
        }
        return jsonFeedback;
    }

    /**
     * 找船反馈（只能看十天）
     * @param request
     * @return
     */
    @RequestMapping("/selectShipFeedBack")
    @ResponseBody
    public String selectShipFeedBack(@RequestParam(value = "page")String page, @RequestParam(value = "limit")String limit, HttpServletRequest request) throws Exception {
        logger.info("FindShipFeedbackController selectShipFeedBack start");
        Map<String,Object> map = new HashMap<>();
        try {
            ParamDto paramDto = new ParamDto();
            PageUtil.setPage(Integer.parseInt(page),Integer.parseInt(limit),paramDto);
            map = iFindShipFeedbackServices.selectShipFeedBack(request,paramDto);
            map.put("code","");
            map.put("msg","");
        } catch (Exception e) {
            logger.error("FindShipFeedbackController selectShipFeedBack error", e);
            map.put("msg","查询失败");
            map.put("code","");
        }
        return new ObjectMapper().writeValueAsString(map);
    }
}
